package com.ruoyi.web.controller.map;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.MapTalent;
import com.ruoyi.system.service.IMapTalentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 人才总览Controller
 *
 * @author cqust_icc
 * @date 2022-06-30
 */
@RestController
@RequestMapping("/system/talent")
public class MapTalentController extends BaseController {
    @Autowired
    private IMapTalentService mapTalentService;

    /**
     * 查询人才总览列表
     */
    @GetMapping("/list")
    public TableDataInfo list(MapTalent mapTalent) {
        startPage();
        List<MapTalent> list = mapTalentService.selectMapTalentList(mapTalent);
        return getDataTable(list);
    }

    /**
     * 导出人才总览列表
     */
    @PreAuthorize("@ss.hasPermi('system:talent:export')")
    @Log(title = "人才总览", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MapTalent mapTalent) {
        List<MapTalent> list = mapTalentService.selectMapTalentList(mapTalent);
        ExcelUtil<MapTalent> util = new ExcelUtil<MapTalent>(MapTalent.class);
        util.exportExcel(response, list, "人才总览数据");
    }

    /**
     * 获取人才总览详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:talent:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(mapTalentService.selectMapTalentById(id));
    }

    /**
     * 新增人才总览
     */
    @PreAuthorize("@ss.hasPermi('system:talent:add')")
    @Log(title = "人才总览", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MapTalent mapTalent) {
        return toAjax(mapTalentService.insertMapTalent(mapTalent));
    }

    /**
     * 修改人才总览
     */
    @PreAuthorize("@ss.hasPermi('system:talent:edit')")
    @Log(title = "人才总览", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MapTalent mapTalent) {
        return toAjax(mapTalentService.updateMapTalent(mapTalent));
    }

    /**
     * 删除人才总览
     */
    @PreAuthorize("@ss.hasPermi('system:talent:remove')")
    @Log(title = "人才总览", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(mapTalentService.deleteMapTalentByIds(ids));
    }

    @Log(title = "人才总览批量导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:talent:add')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<MapTalent> util = new ExcelUtil<>(MapTalent.class);
        List<MapTalent> baseList = util.importExcel( file.getInputStream());
        String message = mapTalentService.importData(baseList);
        return AjaxResult.success(message);
    }
}
